#!/usr/bin/env python
#coding=utf-8

import os
import time
import logging

from common import excel_folder_path

from xsl import XLSWriter
from vic.exchange.vic_huobi.huobi_http import HuobiHttp
from vic.exchange.vic_okex.okex_http import OkexHttp
from vic.exchange.vic_binance.binance_http import BinaHttp
from vic.exchange.vic_okex.okexft_http import OkexftHttp

from risk_netvalue.accounts import huo_accounts, bian_accounts, okex_accounts, collect_accounts, collect_bat

class CollectService(object):
    """BinanService"""
    def __init__(self):
        super(CollectService, self).__init__()
        self.OkexHttp     = OkexHttp()
        self.data_list    = []

    def collect_list(self):
        """获取汇总列表"""

        self.currency_list = self.OkexHttp.currency_list()
        self.bian_list     = bian_accounts()
        self.huobi_list    = huo_accounts()
        self.okex_list     = okex_accounts()

        for currency in self.currency_list:
            data_list = []
            bian_balance, bian_to_usdt, okex_balance, okex_to_usdt, huo_balance, huo_to_usdt = 0, 0, 0, 0, 0, 0
            for b in bian_list:
                if b[0] == currency:
                    bian_balance += float(b[2]) # 持仓数量
                    bian_to_usdt += float(b[3]) # 折合usdt

            for o in okex_list:
                if o[0] == currency:
                    okex_balance += float(o[2]) # 持仓数量
                    okex_to_usdt += float(o[3]) # 折合usdt

            for h in huobi_list:
                if h[0] == currency:
                    huo_balance += float(h[2]) # 持仓数量
                    huo_to_usdt += float(h[3]) # 折合usdt

            sum_balance = float(bian_balance) + float(okex_balance) + float(huo_balance)
            sum_to_usdt = float(bian_to_usdt) + float(okex_to_usdt) + float(huo_to_usdt)

            #print('sum_balance:%s'% sum_balance)
            #print('sum_to_usdt:%s'% sum_to_usdt)

            if sum_balance <= 0:
                continue

            data_list.append(currency.upper())
            data_list.append(sum_balance)
            data_list.append(sum_to_usdt)
            data_list.append(time.strftime('%Y-%m-%d %H:%M',time.localtime()))
            self.data_list.append(data_list)

        return (bian_list, huobi_list, okex_list, self.data_list)

